package com.qianniu.napi;

//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.cache.annotation.EnableCaching;

import org.eclipse.jetty.alpn.ALPN;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;
import java.util.Collections;

//移除 @SpringBootApplication and @ComponentScan, 用 @EnableAutoConfiguration 来替代
//@SpringBootApplication
//@Configuration
//@EnableAutoConfiguration
@EnableCaching
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Value("${server.port}")
    private int serverPort;

    @Value("${server.http.port}")
    private int serverHttpPort;

    /**
     * <p>
     * 测试 RUN
     * <br>
     * 1、http://localhost:8080
     * 2、http://localhost:8080/user/test1<br>
     * </p>
     */
    public static void main(String[] args) {
//		System.setProperty("org.mortbay.jetty.webapp.WebAppContext.maxFormContentSize",800000);
//		System.setProperty("org.mortbay.jetty.webapp.WebAppContext.maxFormContentSize",800000);
//        SpringApplication.run(Application.class, args);
        ALPN.debug = true;
        SpringApplication.run(Application.class, args);
    }

    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);

        // This will set to use COOKIE only
//        servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
        servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
        // This will prevent any JS on the page from accessing the
        // cookie - it will only be used/accessed by the HTTP transport
        // mechanism in use
        SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
        sessionCookieConfig.setHttpOnly(false);
//        sessionCookieConfig.setHttpOnly(true);
    }


    /**
     *jetty: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
     * @return
     */
//	@Bean
//	public EmbeddedServletContainerFactory servletContainer() {
//		JettyEmbeddedServletContainerFactory factory =
//				new JettyEmbeddedServletContainerFactory();
//
//		return factory;
//	}


//    @Bean
//    UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
//        UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
//        factory.addBuilderCustomizers(
//                builder -> builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true),
//                builder -> builder.addHttpListener(serverHttpPort, "0.0.0.0"));
//        return factory;
//    }

}
